
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
 // ERROR: Not supported in C#: OptionDeclaration
namespace NCS_CS
{
	internal class CCard9111
	{
//Implements CCard
//Const demo = False
//Const cardType = PCI_9111HR
//Public needUpdate As Boolean
//Public errmsg As String
		public bool islinked;
		bool[] ChannelOn = new bool[17];
		int[] range_value = new int[9];
		double[] range_low = new double[9];
		double[] range_r = new double[9];
//Dim Active As Integer
//Dim NewStart As Integer
		int[] Status = new int[17];
//Dim angle As Single
//Dim sq As int
//Dim gnAddr As Integer
//Dim gnIrq As Integer
//Dim gnDoValue As Integer
		int gain=0;
		short card;
//Dim stime As Single
		public bool start()
		{
			link();
			return islinked;
		}
		public void link()
		{
			try {
				//Dim result As int
				if (!ModMain.program1.demo) {
					card = DASK.Register_Card(DASK.PCI_9111HR, 0);
					DASK.AO_9111_Config((short)card, DASK.P9111_AO_BIPOLAR);
					//P9111_AO_BIPOLAR)
				}
				if (card < 0) {
					modLog.savelogmsg("register 9111 card failed.");
				} else {
					islinked = true;
				}
				//gnDoValue = 0
				return;
			} catch (System.Exception e) {
				islinked = false;
				// RaiseEvent myErr(1, "can not link card!")
				modLog.savelogmsg(ModMain.gtran.MyTranslate("9111 init ",  "card 9111 init"));
				modLog.savelogmsg(e.ToString()); 
			}
		}
		public void unLink()
		{
			try {
				//Form1.Visible = False
				int result = 0;
				if (!ModMain.program1.demo) {
					if (card >= 0) {
						result = DASK.Release_Card((short)card);
					}
					System.Environment.Exit(0);
					//Set t1 = Nothing
				}
				return;
			} catch (System.Exception e) {
				modLog.savelogmsg(e.ToString()); 
			}
		}
		public void newDA( int chanel,  double v)
		{
			if (!ModMain.program1.demo) {
				DASK.AO_VWriteChannel(card,(short) chanel, v);
			}
		}
		//private Function getchanel(chanel As int) As Single
		//
		//End Function
		//Public Function getChanel()
		//
		//End Function
		public double getchanelVolt( int chanel)
		{
			int result = 0;
			double p = 0;
			if (!ModMain.program1.demo) {
				//result = DASK.AI_VReadChannel(card, (short)chanel,(short) range_value[gain], ref p);
				return p;
			} else {
                System.Random s = new System.Random();
				p = s.NextDouble() + chanel;
			}
			return p;
		}


		public void newDO( byte chanel,  int v)
		{
			if (!ModMain.program1.demo) {
				chanel =(byte) DASK.P9111_CHANNEL_DO;
				v = DASK.DO_WritePort(card, chanel, v);
			}
		}
		public void Di( int Port, ref int v)
		{
			if (!ModMain.program1.demo) {
				DASK.DI_ReadPort(card, (short)Port, ref v);
			}
		}
		public CCard9111() : base()
		{
			int i = 0;
			//   t1.Enabled = False
			for (i = 0; i <= 15; i++) {
				ChannelOn[i] = false;
			}
			for (i = 0; i <= 15; i++) {
				Status[i] = 0;
			}
			range_value[0] = 1;
			range_value[1] = 2;
			range_value[2] = 3;
			range_value[3] = 4;
			range_value[4] = 5;
			//9111
			range_r[0] = 20;
			range_r[1] = 10;
			range_r[2] = 5;
			range_r[3] = 2.5F;
			range_r[4] = 1.25F;
			range_low[0] = -10;
			range_low[1] = -5;
			range_low[2] = -2.5F;
			range_low[3] = -1.25F;
			range_low[4] = -0.625F;
			//gain = 0 '2
			//NewStart = True
			//Active = False

		}

        //protected override void Finalize()
        //{
        //    base.Finalize();
        //}
		//
	}
}
